﻿using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Globalization;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Text;
namespace ClassMedataBrowser.Domain
{
	public class ColumnsDescriptorConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			return true;// return sourceType == typeof(string);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return true; //return destinationType == typeof(string);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
		{
			//string stringVal = value.ToString();

			//if (stringVal == string.Empty)
			//    return new ColumnsDescriptor() { NotSetup = true };

			//try
			//{
			//    using (StringReader writer = new StringReader(stringVal))
			//    {
			//        XmlSerializer serializer = new XmlSerializer(typeof(ColumnsDescriptor));
			//        return (ColumnsDescriptor)serializer.Deserialize(writer);
			//    }
			//}
			//catch
			//{
			//    return new ColumnsDescriptor() { NotSetup = true };
			//}
			return string.Empty;
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
		{
			//using (MemoryStream memoryStream = new MemoryStream())
			//{
			//    XmlSerializer serializer = new XmlSerializer(typeof(ColumnsDescriptor));
			//    serializer.Serialize(memoryStream, value);
			//    memoryStream.Flush();
			//    memoryStream.Seek(0, 0);

			//    using (StreamReader reader = new StreamReader(memoryStream))
			//    {
			//        string xml = reader.ReadToEnd();
			//        return xml;
			//    }
			//}

			return new ColumnsDescriptor();
		}
	}

	[TypeConverter(typeof(ColumnsDescriptorConverter))]
	public class ColumnsDescriptor
	{
		public bool NotSetup
		{
			get;
			set;
		}

		private List<string> _visibleColumns = new List<string>();
		private Dictionary<string, int> _columnWidth = new Dictionary<string, int>();

		public List<string> VisibleColumns
		{
			get { return _visibleColumns; }
		}

		public void ShowColumn(string columnName)
		{
			_visibleColumns.Add(columnName);
		}

		public void HideColumn(string columnName)
		{
			_visibleColumns.Remove(columnName);
		}

		public void SetColumnWidth(string columnName, int columnWidth)
		{
			_columnWidth[columnName] = columnWidth;
		}
	}
}
